package com.javakc.pms.order.dispord.controller;

import com.alibaba.excel.EasyExcel;
import com.javakc.pms.order.dispord.entity.DispOrd;
import com.javakc.pms.order.dispord.listener.ExcelListener;

import com.javakc.pms.order.dispord.vo.DispOrdData;
import com.javakc.pms.order.dispord.vo.DispOrdQuery;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.javakc.pms.dispord.service.DispOrdService;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/pms/dispord")
@Api(tags = "调度指令库接口")
public class DispOrdController {
    @Autowired
    private DispOrdService dispOrdService;

    @GetMapping
    @ApiOperation("查询所有数据")
    public List<DispOrd> queryAll() {
        return dispOrdService.queryAll();
    }

    @ApiOperation("带分页的条件查询")
    @PostMapping("{pageNo}/{pageSize}")
    public Page<DispOrd> queryByPage(
            @ApiParam(name = "pageNo", value = "页码", required = true)
            @PathVariable("pageNo") Integer pageNo,
            @ApiParam(name = "pageSize", value = "每页最大条数", required = true)
            @PathVariable("pageSize") Integer pageSize,
            @ApiParam(name = "dispOrdQuery", value = "查询条件封装类", required = false)
            @RequestBody(required = false) DispOrdQuery dispOrdQuery) {
        return dispOrdService.queryByPage(pageNo, pageSize, dispOrdQuery);
    }

    @ApiOperation(value = "新增调度指令库")
    @PostMapping("saveDispOrd")
    public void saveDispOrd(@RequestBody @Valid DispOrd dispOrd) {
        dispOrdService.save(dispOrd);
    }

    @ApiOperation(value = "根据ID获取调度指令库")
    @GetMapping("{id}")
    public DispOrd queryById(@PathVariable(name = "id") Integer id) {
        return dispOrdService.getById(id);
    }

    @ApiOperation(value = "修改调度指令库")
    @PutMapping("updateDispOrd")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键id",required = true),
            @ApiImplicitParam(name = "priority",value = "指令优先级",required = true),
            @ApiImplicitParam(name = "orderType",value = "专业类型",required = true),
            @ApiImplicitParam(name = "orderName",value = "指令名称",required = true),
            @ApiImplicitParam(name = "orderDescribe",value = "指令描述",required = true)
    })
    public void updateById(@RequestBody @Valid DispOrd dispOrd) {
        dispOrdService.saveOrUpdate(dispOrd);
    }

    @ApiOperation(value = "根据ID删除调度指令库")
    @DeleteMapping("{id}")
    public void deleteById(@PathVariable(name = "id") Integer id) {
        dispOrdService.removeById(id);
    }
    @ApiOperation(value = "Excel导出",notes = "使用了阿里巴巴Excel技术实现Excel导出")
    @GetMapping("exportEasyExcel")
    public void exportEasyExcel(HttpServletResponse response){
    //1.Excel的生产
        //数据库数据
        List<DispOrd> dispOrdList = dispOrdService.queryAll();

        //表头
        List<DispOrdData> dispOrdDataList = new ArrayList<>();

        for (int i = 0; i < dispOrdList.size() ; i++) {

            //取到数据放入实体类
            DispOrd dispOrd = dispOrdList.get(i);
            //需要导出的实体类
            DispOrdData dispOrdData = new DispOrdData();
            //利用BeanUtils.copyProperties放入导出实体类中
            BeanUtils.copyProperties(dispOrd,dispOrdData);
            //放入准备好的导出实体类集合中
            dispOrdDataList.add(dispOrdData);

        }
        //2.Excel的下载

        String fileName = "list";
        try {
            //2.1下载准备，解决乱码
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + ".xlsx");
            //2.2下载完成
            EasyExcel.write(response.getOutputStream(),DispOrdData.class).sheet().doWrite(dispOrdDataList);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @ApiOperation(value = "Excel数据导入",notes = "利用阿里巴巴Excel技术实现数据导入")
    @PostMapping("importEasyExcel")
    public void importEasyExcel(MultipartFile file){
        //1.数据导入要有一个监听器 ExcelListener.
        //2.保存
        try {
            EasyExcel.read(file.getInputStream(),DispOrdData.class,new ExcelListener(dispOrdService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @ApiOperation(value = "Excel导出",notes = "使用了POI技术完成Excel导出实现")
    @GetMapping("exportExcel")
    public void exportExcel(HttpServletResponse response){
        dispOrdService.exportExcel(response);
    }
    @ApiOperation(value = "Excel数据导入",notes = "利用POI技术完成Excel数据导入")
    @PostMapping("importExcel")
    public void importExcel(MultipartFile file){
        dispOrdService.importExcel(file);
    }
}
